#include <iostream>
#include <ctime>
#include <cstdlib>
#include <malloc.h>
using namespace std;

#define MAXSIZE 10

typedef struct{
	int data[MAXSIZE];	//存放栈元素，maxsize是已定义的常量
	int top;	//栈顶指针
}SqStack;

void initStack(SqStack&);	//初始化栈
int isEmpty(SqStack);		//判断栈空代码
int push(SqStack&, int);	//入栈代码
int pop(SqStack&, int&);	//出栈代码

int main(void)
{
	srand((unsigned)time(NULL));
	SqStack st;
	initStack(st);  //初始化栈
	for(int i = 0; i < MAXSIZE; i++)
	{
		push(st,i);
	}
	cout << "finish!" << endl;
	//push(st,6);
	int res = -1;
	for(int i = 0; i < 20; i++)
	{
		if(pop(st,res))cout << "出栈元素是 " << res <<endl;
	}
	pop(st,res);
	
	return EXIT_SUCCESS;
}

//判断栈空
void initStack(SqStack &st)
{
	st.top = -1;	//只需将栈顶指针设置为-1
}

int isEmpty(SqStack st)
{
	if (st.top == -1)return 1;
	else return 0;
}

//入栈
int push(SqStack &st, int x)
{
	if (st.top == MAXSIZE - 1)
	{
		cout << "栈满，入栈失败！" << endl;
		return 0;
	}
	else
	{
		st.data[++st.top] = x;	//先增再用
		return 1;
	}
}

//出栈
int pop(SqStack &st, int &x)
{
	if (st.top == -1)
	{
		cout << "栈空，出栈失败！" << endl;
		return 0;
	}
	else
	{
		x = st.data[st.top--];	//用完再减
		return 1;
	}
}

